package com.ncu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ncu.mapper.EmpMapper;
import com.ncu.pojo.Emp;
import com.ncu.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> findAll() {
        List<Emp> emp = empMapper.findAll();
        return emp;
    }

    @Override
    public PageInfo findAllDept(int currentPage) {
        PageHelper.startPage(currentPage,2);
        List<Emp> allEmp = empMapper.findAllDept();
        PageInfo page=new PageInfo(allEmp);
        return page;
    }

    @Override
    public int saveEmp(Emp emp) {
        String regName="^[\\u4E00-\\u9FA5A-Za-z0-9_-]{2,12}$";
        String regEmail="^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
        if(emp.getEmpName().matches(regName)&&emp.getEmail().matches(regEmail)){
            return empMapper.saveEmp(emp);
        }else {
            return -1;
        }


    }

    @Override
    public int checkEmpName(String empName) {
        List<Emp> emps = empMapper.checkEmpName(empName);

        return emps.size();
    }

    @Override
    public Emp findEmpById(int empId) {
        Emp emp = empMapper.findEmpById(empId);
        return emp;
    }

    @Override
    public int updateEmp(Emp emp) {
        String regEmail="^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
        if(emp.getEmail().matches(regEmail)){
            return empMapper.updateEmp(emp);
        }else {
            return -1;
        }


    }

    @Override
    public int deleteEmp(int empId) {
        return empMapper.deleteEmp(empId);
    }

    @Override
    public int deleteBatch(List<Integer> listEmp) {
        return empMapper.deleteBatch(listEmp);
    }


}
